package com.qinggeng.framework.config.mq.core;

import com.alibaba.fastjson.JSONObject;
import com.qinggeng.common.utils.CalendarAdjustUtils;
import com.qinggeng.framework.config.mq.core.mapper.GsdMessageMapper;
import com.qinggeng.framework.config.mq.model.MessageModel;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.Date;

@Service
public class GsdMessageServiceImpl implements  GsdMessageService {
    @Resource
    private GsdMessageMapper gsdMessageMapper;

    @Override
    public boolean insertGsdMessage(MessageModel message) {
        GsdMessage gsdMessage =new GsdMessage();
        Long messageId=message.getMessageId();
        gsdMessage.setMessageId(messageId);
        Example example=new Example(GsdMessage.class);
        example.createCriteria().andEqualTo("messageId",messageId);
        GsdMessage gsd=gsdMessageMapper.selectOneByExample(example);
        if (gsd!=null){
            return  true;
        }
        gsdMessage.setContent(JSONObject.toJSONString(message));
        Date date=new Date();
        gsdMessage.setCreateDate(date);
        gsdMessage.setCreateTime(date);
        Instant instant = Instant.now();
        long timeStampMillis = instant.toEpochMilli();
        gsdMessage.setUpdateTime(date);
        gsdMessage.setCreateTimeStamp(timeStampMillis);
        gsdMessage.setCreateYear(CalendarAdjustUtils.getYearToStr());
        gsdMessage.setCreateMonth(CalendarAdjustUtils.getMonthToStr());
        gsdMessage.setType(message.getType());
        return gsdMessageMapper.insertSelective(gsdMessage)>=1;
    }

    @Override
    public boolean deleteGsdMessage(String messageId) {
        Example example =new Example(GsdMessage.class);
        example.createCriteria().andEqualTo("messageId",messageId);
        return gsdMessageMapper.deleteByExample(example)>=1;
    }
}
